import https from "https";
import { load } from "cheerio";
import path from "path";
import fs from "fs";

// 获取页面内容
const fetchPage = (selector, url) =>
  https
    .get(url, (response) => {
      let html = "";

      // 监听数据传输事件，收集HTML内容
      response.on("data", (chunk) => {
        html += chunk;
      });

      // 监听数据传输结束事件，处理HTML内容
      response.on("end", () => {
        // 提取图片链接
        const $ = load(html);
        // 下载图片
        $(selector).each((index, element) => {
          const imageUrl = $(element).attr("src");
          imageUrl && saveImg(imageUrl, index);
        });
      });
    }) // 监听请求错误事件
    .on("error", (error) => {
      console.error("[抓取页面错误]:", error);
    });

// 下载图片的函数
function saveImg(imgUrl, index) {
  // 发起HTTP GET请求
  https
    .get(imgUrl, (response) => {
      const directory = "./httpsIMG/";
      if (!fs.existsSync(directory)) {
        makeDirs(directory);
      }
      // 创建写入流
      const filePath = path.join(
        process.cwd(),
        directory + `image_${index}.jpg`
      );

      const fileStream = fs.createWriteStream(filePath);

      // 监听数据传输事件，将图片数据写入文件
      response.pipe(fileStream);

      // 监听文件流事件
      fileStream.on("error", (error) => {
        console.error(`[写入错误] image_${index} to disk:`, error);
      });
      fileStream.on("finish", () => {
        console.log(`Image_${index} 下载成功!`);
      });
    })
    .on("error", (error) => {
      console.error(`[下载中错误]: image_${index}:`, error);
    });
}
// 创建所需目录
function _makeDirs(dirname) {
  if (fs.existsSync(dirname)) return true;

  if (_makeDirs(path.dirname(dirname))) {
    try {
      fs.mkdirSync(dirname);
      return true;
    } catch (error) {
      console.error("Error creating directory:", error);
      return false;
    }
  }
  return true;
}

export { fetchPage };
